import Koa from 'koa'
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import cors from '@koa/cors'
import mysql from 'mysql2/promise'
const pool = mysql.createPool({
    database:'bookDB',
    user:'root',
    password:'123456',
    host:'localhost'
})
const app =new Koa()
const router =new Router()
app.use(bodyParser())
app.use(cors())
router.get('/',async(ctx)=>{
const [rows] = await pool.query('select * from book')
ctx.body=rows
})
router.delete('/:id',async(ctx)=>{
    await pool.query('delete from book where id=?',[ctx.params.id])
})
router.post('/',async(ctx)=>{
    const {name,className} =ctx.request.body
    const [result] = await pool.query('insert into book (name,className) values (?,?)',[name,className])
    ctx.body={id:result.insertId}
})
router.put('/:id',async(ctx)=>{
    await pool.query('updata book set name=? className=? where id =?',[ctx.params.id])
})
app.use(router.routes())
app.listen(8080,()=>{
    console.log('http://localhost:8080')
})